package com.bz.sunmipay.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.baizhi.baseapp.activity.BaseActivity;
import com.baizhi.baseapp.util.JsonUtils;
import com.baizhi.baseapp.widget.WToast;
import com.bz.sunmipay.R;
import com.bz.sunmipay.adapter.HomeInfoAdapter;
import com.bz.sunmipay.application.MyApplication;
import com.bz.sunmipay.model.HomeInfo;
import com.bz.sunmipay.payment.PaymentHelp;
import com.bz.sunmipay.utils.IntentUtils;

import java.util.List;

import de.wirecard.paymentsdk.WirecardPaymentResponse;
import de.wirecard.paymentsdk.WirecardResponseError;
import de.wirecard.paymentsdk.WirecardResponseListener;
import sunmi.paylib.SunmiPayKernel;

public class MainActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private static final String TAG = "WireCard";
    private static final long PRESS_DURATION = 1000;
    private long backTime;
    private boolean isExit;

    private HomeInfoAdapter mAdapter;

    private SunmiPayKernel mSMPayKernel;
    private boolean isDisConnectService = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_main);
        setCenterText("HOME");

        initView();
        connectPayService();
    }

    private void connectPayService() {
        mSMPayKernel = SunmiPayKernel.getInstance();
        mSMPayKernel.initPaySDK(this, mConnectCallback);
    }

    private SunmiPayKernel.ConnectCallback mConnectCallback = new SunmiPayKernel.ConnectCallback() {

        @Override
        public void onConnectPaySDK() {
            Log.e("dd--", "onConnectPaySDK");
            try {
                MyApplication.mEMVOptV2 = mSMPayKernel.mEMVOptV2;
                MyApplication.mBasicOptV2 = mSMPayKernel.mBasicOptV2;
                MyApplication.mPinPadOptV2 = mSMPayKernel.mPinPadOptV2;
                MyApplication.mReadCardOptV2 = mSMPayKernel.mReadCardOptV2;
                MyApplication.mSecurityOptV2 = mSMPayKernel.mSecurityOptV2;
                MyApplication.mTaxOptV2 = mSMPayKernel.mTaxOptV2;
                isDisConnectService = false;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onDisconnectPaySDK() {
            Log.e("dd--", "onDisconnectPaySDK");
            isDisConnectService = true;
            Toast.makeText(MainActivity.this, "Disconnected from payment service, please exit and reconnect", Toast.LENGTH_SHORT).show();
        }

    };

    private void initView() {
        List<HomeInfo> homeInfos = JsonUtils.parseJson(this, "main_config", HomeInfo[].class);
        final GridView gridView = findViewById(R.id.home_grid);
        gridView.setOnItemClickListener(this);
        mAdapter = new HomeInfoAdapter(this);
        mAdapter.setItems(homeInfos);
        gridView.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.e("dd--", "onItemClick  "+position);
        if (position == 0 && isDisConnectService) {
            connectPayService();
            Toast.makeText(MainActivity.this, "Connecting to payment service...", Toast.LENGTH_SHORT).show();
            return;
        }
        HomeInfo item = mAdapter.getItem(position);
        IntentUtils.goToHomeItemPager(this, item);
//        test();
    }

    private void test() {
        showProgress("Creating card information");
        PaymentHelp.getInstance(this).makeSimplePayment("4271149787014678", "", "0.01", new WirecardResponseListener() {
            @Override
            public void onResponse(WirecardPaymentResponse wirecardPaymentResponse) {
                dismissProgress();
                Log.e(TAG, "onMessage: " + wirecardPaymentResponse.getTransactionState().getValue());
                Log.e(TAG, "onDesc: " + wirecardPaymentResponse.getStatuses().getStatus()[0].getDescription());
                //resultLabel.setText(wirecardPaymentResponse.getTransactionState().getValue() + "\n"
                // wirecardPaymentResponse.getStatuses().getStatus()[0].getDescription());

                WToast.showToast(wirecardPaymentResponse.getTransactionState().getValue());
            }

            @Override
            public void onError(WirecardResponseError wirecardResponseError) {
                dismissProgress();
                WToast.showToast(wirecardResponseError.getErrorMessage());
                Log.e(TAG, "onErrorMessage: " + wirecardResponseError.getErrorMessage());
                Log.e(TAG, "onErrorCode: " + wirecardResponseError.getErrorCode());
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - backTime > PRESS_DURATION) {
            isExit = false;
        }
        backTime = System.currentTimeMillis();
        if (isExit) {
            super.onBackPressed();
        } else {
            isExit = true;
            showMessage("please click it again exit app");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSMPayKernel != null) {
            mSMPayKernel.destroyPaySDK();
        }
    }
}
